package com.course.action;

import java.io.IOException;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.Vector;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.course.bean.SolutionTo;
import com.course.delegate.SolutionMgrDelegate;
import com.course.exception.DataNotFoundException;
import com.course.formbean.SolutionFormBean;
import com.course.util.UtilConstants;



public class ViewSolutionAction extends HttpServlet {

	private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	          doPost(request, response);
	}
public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

             SolutionFormBean sb=new SolutionFormBean();
		
		
		Map map=request.getParameterMap();
	try {
		BeanUtils.populate(sb, map);
	} catch (IllegalAccessException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	} catch (InvocationTargetException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
		}
        String path="";
        Vector<SolutionTo> getsolution=null;

	  try{
		  
		  int qid=Integer.parseInt(request.getParameter("qid"));
		  String login=request.getParameter("from");
		  try{
		  getsolution= new SolutionMgrDelegate().getSolution(qid,login);
		  }catch (DataNotFoundException e) {
			e.printStackTrace();
		}
		  System.out.println("in Action class vcb..........."+getsolution);
		  if(!getsolution.isEmpty()){
			  request.setAttribute("Solutionvector", getsolution);
			  request.setAttribute("status", UtilConstants._POST_SOLUTION_INFO);
			  path=UtilConstants._VIEW_SOLUTION;
			 }
		  else {
			  request.setAttribute("status", UtilConstants._NO_SOLUTION);
			  path=UtilConstants._VIEW_SOLUTION;
		}
		}
	  catch (NullPointerException e) {
		  request.setAttribute("status", UtilConstants._NO_SOLUTION);
		  path=UtilConstants._VIEW_SOLUTION;
		e.printStackTrace();
	}
	RequestDispatcher rd=request.getRequestDispatcher(path);
		rd.forward(request,response);

}

	}


